// 右键菜单
;layui.define(['tree'], function(exports) {
	var $ = layui.$;
	
	// 获得所有的节点ID
	function findChildrenId(nodes, menuId) {
        for (var key in nodes) {
            var node = nodes[key];
            if (node) {
                if (node.id) {
                    menuId.push(node);
                }
                if (node.children && node.children.length > 0) {
                    findChildrenId(node.children, menuId);
                }
            }
        }
    }
		
	exports('contextTree', {
		menu: function(cfg) {
			// 需要nodes参数
	        var menuIds = [];
	        findChildrenId(cfg.nodes, menuIds);
	        for(var i=0; i<menuIds.length; i++) {
	        	$.contextMenu({
                    items: cfg.items,
                    treeNode: menuIds[i],
                    selector: '#layui_treeId' + menuIds[i].id, //右键选择器
                    callback: function (item, options) { //点击回调处理
                    	var it = cfg.items[item];
                    	if(it) {
                    		it.handle(options.treeNode);                    	
                    	}                    	
                    }                    
                });
	        }
		}
	});
});

